#!/bin/sh
# $Id$
# Public domain.  Originally written many years ago by Sebastian Rahtz.
# The basic idea is to run configure && make, but with a multitude of
# environment variables to allow overriding everything.
# 
# To build again from where it left off, try Build --no-clean.
# To build without optimization, try Build --debug.
# Any other options given are passed along to configure.

# clean up environment
unset TEXMFCNF; export TEXMFCNF
LANG=C; export LANG

# cd to our source directory.
mydir=`dirname $0`
cd $mydir || exit 1
mydir=`pwd` # make absolute

# We export the TL_* variables so that they can be dumped to buildenv.log.
: ${TL_WORKDIR=Work}; export TL_WORKDIR

# allow override of install destination.
if test -z "$TL_INSTALL_DEST"; then
  H=`pwd`
  test -d inst || mkdir -p inst/texmf  # avoid configure warnings
  TL_INSTALL_DEST=$H/inst
fi
export TL_INSTALL_DEST

# allow override of the make program.
# The idea is to use TL_MAKE if that is defined (and set MAKE),
# or MAKE if that is defined (and set TL_MAKE),
# or default to "make" if neither is set.
# We have to end up with both defined because TL_MAKE is used below
# in this script, and MAKE is used throughout (in its usual way).
if test -n "$TL_MAKE"; then
  MAKE=$TL_MAKE
elif test -n "$MAKE"; then
  TL_MAKE=$MAKE
else
  TL_MAKE=make
  MAKE=make
fi
export MAKE; export TL_MAKE

# make flags
: ${TL_MAKE_FLAGS=}; export TL_MAKE_FLAGS
: ${TL_MAKE_VERBOSE=VERBOSE=1}; export TL_MAKE_VERBOSE

# allow override of make target.
: ${TL_TARGET=world}; export TL_TARGET

if test "x$1" = x--no-clean; then
  shift
  echo "$0: --no-clean given, so keeping build and install dirs: " \
       "$TL_WORKDIR $TL_INSTALL_DEST"
else
  test -f Makefile && $MAKE clean 
  rm -rf $TL_WORKDIR $TL_INSTALL_DEST
fi

# allow adding environment setting for build.
: ${TL_BUILD_ENV=}; export TL_BUILD_ENV
if test "x$1" = x--debug || test "x$1" = x-g; then
  shift
  # The idea is that with Build -g, you can set TL_COMPILER_GFLAGS in
  # the environment with options common to all compilers --
  # not necessarily anything to do with debugging, e.g., -mcpu=sparvc9.
  # Or you can set TL_{C,CXX,OBJCXX}FLAGS for per-language flags,
  # notably TL_CFLAGS=-Wdeclaration-after-statement.
  : ${TL_COMPILER_GFLAGS=-g}; export TL_COMPILER_CFLAGS
  c="CFLAGS='$TL_COMPILER_GFLAGS $TL_CFLAGS'"
  cxx="CXXFLAGS='$TL_COMPILER_GFLAGS $TL_CXXFLAGS'"
  objcxx="OBJCXXFLAGS='$TL_COMPILER_GFLAGS $TL_OBJCXXFLAGS'" # only for macs
  TL_BUILD_ENV="$c $cxx $objcxx $TL_BUILD_ENV"
fi

# allow for changing the banner identification, e.g.,
# --with-banner-add='/SomeDistro'; see the build doc.
: ${TL_CONF_BANNER=}; export TL_CONF_BANNER

# default to supporting large files as much as possible;
# see comments at --disable-largefile in README.config.
: ${TL_CONF_LARGEFILE=--enable-largefile}; export TL_CONF_LARGEFILE

# default to terminate if requested programs or features must be disabled.
: ${TL_CONF_MISSING=--disable-missing}; export TL_CONF_MISSING

# default to static linking.
: ${TL_CONF_SHARED=--disable-shared}; export TL_CONF_SHARED

# allow override of xdvi toolkit, default to standard xaw.
: ${TL_CONF_XDVI_TOOLKIT=--with-xdvi-x-toolkit=xaw}
export TL_CONF_XDVI_TOOLKIT

# allow override of configure location.
: ${TL_CONFIGURE=../configure}; export TL_CONFIGURE

# allow adding arbitrary other configure args, after all the others.
: ${TL_CONFIGURE_ARGS=}; export TL_CONFIGURE_ARGS

# allow for doing stuff betwen configure and make.
: ${TL_POSTCONFIGURE=true}; export TL_POSTCONFIGURE

# Kpathsea is not going to be able to find its cnf files during the
# build, so omit the warning about it.
: ${KPATHSEA_WARNING=0}; export KPATHSEA_WARNING

# make our working directory.
test -d $TL_WORKDIR || mkdir $TL_WORKDIR
cd $TL_WORKDIR || exit 1

# configure && make.  Keep the tee outside, so that we can detect
# failure at either step.
{
  echo "starting TeX Live build at `date`"
  printf 'CMDLINE\t"%s"\n' "$0 $*"
  printf 'PWD\t"%s"\n'     "`pwd`"
  printf 'UNAME\t"%s"\n'   "`uname -a`"
  env | sort >buildenv.log
  echo "See also buildenv.log and buildinfo.log in `pwd`"
  echo
  #
  set -vx  # show the configure and make commands in the log.

  eval $TL_BUILD_ENV $TL_CONFIGURE \
        --prefix=$TL_INSTALL_DEST \
        --datadir=$TL_INSTALL_DEST \
        $TL_CONF_BANNER \
        $TL_CONF_MISSING \
        $TL_CONF_LARGEFILE \
        $TL_CONF_SHARED \
        $TL_CONF_XDVI_TOOLKIT \
        $TL_CONFIGURE_ARGS \
        "$@" \
  && eval $TL_POSTCONFIGURE \
  && eval $TL_BUILD_ENV $TL_MAKE $TL_MAKE_FLAGS $TL_MAKE_VERBOSE $TL_TARGET
  
  # Too arcane to propagate the exit status through a pipeline.
  # Just use a temp file.
  echo $? >exitstatus.txt
} 2>&1 | tee build.log
 

# if we have a bindir, report the number of binaries built.
bindir=$TL_INSTALL_DEST/bin
if test -d "$bindir"; then
  count=`find "$bindir" \! -type d -print | wc -l`
  if test "$count" -gt 0; then
    echo
    echo "$0: $count executables in $bindir."
  else
    echo "$0: Build failed, no executables under $bindir."
    echo "$0: Full log in: `pwd`/build.log"
    exit 1
  fi | tee -a build.log
# if no bindir, perhaps they specified --prefix; don't worry.
# Any errors will have been duly reported anyway.
fi

status=`cat exitstatus.txt`
if echo "$status" | egrep '^[0-9]+$' >/dev/null; then :; else
  echo "$0: exitstatus.txt does not contain a number; disk full?" >&2
  status=1
fi
echo "done (exit status $status)" `date` | tee -a build.log

exit $status
